package com.SoilMM.controller;


import com.SoilMM.entity.MtimeMonitEntity;
import com.SoilMM.entity.StandarMonitEntity;
import com.SoilMM.service.MtimeMonitService;
import com.SoilMM.service.StandarMonitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GouCS
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/standarMonit")
public class StandarMonitController {
    @Autowired
    StandarMonitService standarMonitService;
    @Autowired
    MtimeMonitService mtimeMonitService;
    @RequestMapping("addStandar")
    public Map<String,Object> addStandar (@RequestBody StandarMonitEntity standarMonitEntity){
        boolean save = standarMonitService.save(standarMonitEntity);
        Map<String,Object> map = new HashMap<>();
        if (save) {
            map.put("code",6666);
            map.put("message","新增成功");
        }else {
            map.put("message","增加失败");
        }
        return map;
    }
    @RequestMapping("queryOne")
    public Map<String ,Object> queryOne(int id){
        StandarMonitEntity monit = standarMonitService.getById(id);
        Map<String,Object> map = new HashMap<>();
        if (monit != null){
            map.put("code","6666");
            map.put("message","标准数据已加载");
            map.put("data",monit);
        }else map.put("message","加载失败失败");
        return map;
    }

    @RequestMapping("updateStandar")
    public Map<String,Object> updateStandar(@RequestBody StandarMonitEntity standarMonitEntity){
        boolean b = standarMonitService.updateById(standarMonitEntity);
        Map<String,Object> map = new HashMap<>();
        if (b){
            map.put("code","6666");
            map.put("message","修改成功");
        }else map.put("message","修改失败");
        return map;
    }
    @RequestMapping("deleteOneStandar")
    public Map<String,Object> deleteOneStandar (String id){
        boolean b = standarMonitService.removeById(id);
        Map<String,Object> map = new HashMap<>();
        if (b){
            map.put("code","6666");
            map.put("message","删除成功");
        }else map.put("message","删除失败");
        return map;
    }
    @RequestMapping("queryOneData")
    public Map<String ,Object> queryOneData(int date, int standar) throws Exception {
        List<Integer> list = new ArrayList<>();
//        int id = Integer.parseInt(date.substring(8,10));
        StandarMonitEntity standarMonitEntity = standarMonitService.getById(standar);
        MtimeMonitEntity mtimeMonitEntity = mtimeMonitService.getById(date);
            // 获取所有属性
            Field[] fs1 = mtimeMonitEntity.getClass().getDeclaredFields();
            Field[] fs2 = standarMonitEntity.getClass().getDeclaredFields();
            for (int i = 0 ; i <fs1.length ;i++) {
                fs1[i].setAccessible(true);
                fs2[i].setAccessible(true);
                switch (fs1[i].getType().getName()) {
                    case "java.lang.Float":
                        if ((float) fs1[i].get(mtimeMonitEntity) > (float) fs2[i].get(standarMonitEntity)) {
                            list.add(i);

                        }else list.add(0);
                        break;
                    case "java.lang.Integer":
//                        if ((Integer) fs1[i].get(mtimeMonitEntity) > (Integer) fs2[i].get(standarMonitEntity)) {
//                            list.add(i);
//                            }else
//                                list.add(0);
                        break;
                    case "java.lang.Long":
                        if ((long) fs1[i].get(mtimeMonitEntity) > (long) fs2[i].get(standarMonitEntity)) {
                            list.add(i);
                        }else list.add(0);
                        break;
                }
            }
                    //获取各个属性的值
//            for (Field field : fs) {
//                //true代表暴力反射，可以访问private修饰的东西，否则无法访问
//                field.setAccessible(true);
//                Object v1 = field.get(mtimeMonitEntity);
//                Object v2 = field.get(standarMonitEntity);
//                if(v1 instanceof Double && v1 instanceof Double){//值的类型
//                    if (ifOverThreshold(v1, v2)){
//                        list.add("true");
//                    }
//
//                }
//            }
        Map<String,Object> map = new HashMap<>();
        if (mtimeMonitEntity != null){
            map.put("code","6666");
            map.put("message","预测数据已加载");
            map.put("sdData",standarMonitEntity);
            map.put("realData",mtimeMonitEntity);
            map.put("compare",list);
        }else map.put("message","查询失败");
        return map;
    }
}

